package com.hieuzest.citsaw;

import android.location.Location;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.amap.api.maps.AMap;
import com.amap.api.maps.MapView;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.MyLocationStyle;
import com.amap.api.maps.model.PolygonOptions;
import com.hieuzest.citsaw.beam.Jigsaw;
import com.hieuzest.citsaw.beam.Piece;
import com.hieuzest.citsaw.beam.RequestBody;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import rx.Observable;
import rx.Observer;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * Created by Giraffe on 2017/11/25.
 */



public class MapFragment extends Fragment {

    public static final String TAG = "MapFragment";
    public static final int COLOR_MARKED = 0x8800FFFF;
    public static final int COLOR_FOUND = 0x8800FF7F;
    public static final int COLOR_UNFOUND = 0x88808080;

    public static final int TIME_DELAY = 1000;

    private MapView mMapView;
    private AMap aMap;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_map, container, false);
        mMapView = (MapView) view.findViewById(R.id.map_view);
        mMapView.onCreate(savedInstanceState);

        aMap = mMapView.getMap();
        AndroidApplication.getInstance().aMap = aMap;



        aMap.setCustomMapStylePath("/sdcard/mystyle.data");
        aMap.setMapCustomEnable(true);
        MyLocationStyle myLocationStyle = new MyLocationStyle();
        myLocationStyle.interval(1000);
        myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_MAP_ROTATE);
        aMap.setMyLocationStyle(myLocationStyle);
//        aMap.getUiSettings().setMyLocationButtonEnabled(true);
        aMap.setMyLocationEnabled(true);

        aMap.setOnMyLocationChangeListener(new AMap.OnMyLocationChangeListener() {
            @Override
            public void onMyLocationChange(Location location) {
                AndroidApplication.getInstance().CurrentX = location.getLongitude();
                AndroidApplication.getInstance().CurrentY = location.getLatitude();
                AndroidApplication.getInstance().CurrentBearing = aMap.getCameraPosition().bearing;
                if (!JigsawManager.getInstance().login(0)) {
                    List<Jigsaw> lj = JigsawManager.getInstance().getJigsaws();
                    if (lj.size() != 0) {
                        aMap.clear(false);
                        for (Jigsaw j : lj) {
                            Observable.from(j.pieces)
                                    .map(new Func1<Piece, PolygonOptions>() {
                                        @Override
                                        public PolygonOptions call(Piece piece) {
                                            return drawPiece(piece);
                                        }
                                    }).subscribeOn(Schedulers.io())
                                    .observeOn(AndroidSchedulers.mainThread())
                                    .subscribe(new Observer<PolygonOptions>() {
                                        @Override
                                        public void onCompleted() {

                                        }

                                        @Override
                                        public void onError(Throwable e) {
                                            e.printStackTrace();
                                        }

                                        @Override
                                        public void onNext(PolygonOptions polygonOptions) {
                                            aMap.addPolygon(polygonOptions);
                                        }
                                    });
                        }
                    }
                }

                Log.i(TAG, "x : " + location.getLongitude() + " , y : " + location.getLatitude() + " , b :" + aMap.getCameraPosition().toString());
            }
        });
        return view;
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        mMapView.onDestroy();
    }
    @Override
    public void onResume() {
        super.onResume();
       // mMapView.onResume();
    }
    @Override
    public void onPause() {
        super.onPause();
     //   mMapView.onPause();
    }
    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
    //    mMapView.onSaveInstanceState(outState);
    }


    PolygonOptions drawPiece(Piece p) {
//        Log.i(TAG, "drawing " + p.pid +" " +p.x1 + ", " + p.y1 + " |" + p.x2 + ", " + p.y2 + " | " + p.x + "," + p.y);
        List<LatLng> latLngs = new ArrayList<>();
        latLngs.add(new LatLng(p.y1, p.x1));
        latLngs.add(new LatLng(p.y1, p.x2));
        latLngs.add(new LatLng(p.y2, p.x2));
        latLngs.add(new LatLng(p.y2, p.x1));
        int color = COLOR_FOUND;
        switch (p.status) {
            case Piece.STATUS_UNFOUND:
                color = COLOR_UNFOUND;
                break;
            case Piece.STATUS_FOUND:
                color = COLOR_FOUND;
                break;
            case Piece.STATUS_MARKED:
                color = COLOR_MARKED;
                break;
        }
        return new PolygonOptions().addAll(latLngs).fillColor(color).strokeWidth(1);
    }


    void drawJigsaw(Jigsaw jigsaw) {
        aMap.clear();
        for(Piece p : jigsaw.pieces) {
            Log.i(TAG, "drawing " + p.pid + p.x1 + ", " + p.y1 + " |" + p.x2 + ", " + p.y2 + " | " + p.x + "," + p.y);
            List<LatLng> latLngs = new ArrayList<>();
            latLngs.add(new LatLng(p.y1, p.x1));
            latLngs.add(new LatLng(p.y1, p.x2));
            latLngs.add(new LatLng(p.y2, p.x2));
            latLngs.add(new LatLng(p.y2, p.x1));
            int color = COLOR_FOUND;
            switch (p.status) {
                case Piece.STATUS_UNFOUND:
                    color = COLOR_UNFOUND;
                    break;
                case Piece.STATUS_FOUND:
                    color = COLOR_FOUND;
                    break;
                case Piece.STATUS_MARKED:
                    color = COLOR_MARKED;
                    break;
            }
            aMap.addPolygon(new PolygonOptions().addAll(latLngs).fillColor(color).strokeWidth(1));
        }

    }

}
